﻿using FRMS.BUS;
using FRMS.DAL;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiExplorerSettings(GroupName = "v1")]
    [ApiController]
    [Authorize]
    public class BuildController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public BuildController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
          )
        {
            _jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
        }


        /// <summary>
        /// 新建楼栋单元
        /// </summary>
        [HttpPost]
        [Route("BuildAdd")]
        public ResponseMessage BuildAdd(BuildInfo buildInfo)
        {
            ResponseMessage message = new ResponseMessage();
            BuildBUS BUS = new BuildBUS();
            try
            {
                buildInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.AddBuild(buildInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增单元楼栋";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增楼栋单元异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 修改楼栋单元
        /// </summary>
        [HttpPost]
        [Route("BuildEdit")]
        public ResponseMessage BuildEdit(BuildInfo buildInfo)
        {
            ResponseMessage message = new ResponseMessage();
            BuildBUS BUS = new BuildBUS();
            try
            {
                message = BUS.EditBuild(buildInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "修改单元楼栋";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "修改楼栋单元异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 删除单元楼栋信息
        /// </summary>
        [HttpPost]
        [Route("BuildDelete")]
        public ResponseMessage BuildDelete(BuildInfo buildInfo)
        {
            ResponseMessage message = new ResponseMessage();
            BuildBUS BUS = new BuildBUS();
            try
            {
                message = BUS.DeleteBuild(buildInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除单元楼栋";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除楼栋单元异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询楼栋单元
        /// </summary>
        [HttpPost]
        [Route("QueryBuild")]
        public ResponseMessage QueryBuild(BuildInfo buildInfo)
        {
            ResponseMessage message = new ResponseMessage();
            BuildBUS BUS = new BuildBUS();
            try
            {
                buildInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryBuild(buildInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询单元楼栋";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询楼栋单元异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 载入楼栋单元信息
        /// </summary>
        [HttpPost]
        [Route("LoadBuild")]
        public ResponseMessage LoadBuild(BuildInfo buildInfo)
        {
            ResponseMessage message = new ResponseMessage();
            BuildBUS BUS = new BuildBUS();
            try
            {
                message = BUS.LoadBuild(buildInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "载入单元楼栋";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "载入楼栋单元信息异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 测试
        /// </summary>
        [HttpPost]
        [Route("Test")]
        public ResponseMessage Test(ErrorLog errorLog)
        {
            ResponseMessage message = new ResponseMessage();
            DataDAL da = new DataDAL();
            try
            {
                bool result = da.AddErrorLog(errorLog);
                if (!result)
                {
                    message.errcode = 1;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
            }
            return message;
        }
    }
}
